// $ANTLR 3.2 Sep 23, 2009 12:02:23 SQL.g 2012-09-28 13:53:25


  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.Map;



import org.antlr.runtime.*;

public class SQLParser extends Parser {
    public static final String[] tokenNames = new String[] {
        "<invalid>", "<EOR>", "<DOWN>", "<UP>", "Identifier", "Int", "CharString", "Float", "WS", "';'", "'quit'", "'select'", "'from'", "'where'", "'group'", "'by'", "'.'", "'order'", "','", "'as'", "'and'", "'('", "'or'", "')'", "'='", "'<'", "'>'", "'not'", "'sum'", "'avg'", "'asc'", "'dsc'", "'+'", "'-'", "'*'", "'/'"
    };
    public static final int T__29=29;
    public static final int T__28=28;
    public static final int T__27=27;
    public static final int T__26=26;
    public static final int T__25=25;
    public static final int T__24=24;
    public static final int T__23=23;
    public static final int T__22=22;
    public static final int T__21=21;
    public static final int T__20=20;
    public static final int CharString=6;
    public static final int EOF=-1;
    public static final int T__9=9;
    public static final int Int=5;
    public static final int Identifier=4;
    public static final int Float=7;
    public static final int T__30=30;
    public static final int T__19=19;
    public static final int T__31=31;
    public static final int T__32=32;
    public static final int WS=8;
    public static final int T__33=33;
    public static final int T__16=16;
    public static final int T__34=34;
    public static final int T__15=15;
    public static final int T__35=35;
    public static final int T__18=18;
    public static final int T__17=17;
    public static final int T__12=12;
    public static final int T__11=11;
    public static final int T__14=14;
    public static final int T__13=13;
    public static final int T__10=10;

    /**
	 * Returns true if the query for this parser is an aggregation query.
	 * @return
	 */
	public boolean isAggregation()
	{
		if(groupingAtt != null)
		{
			return true;
		}

		if(exprsToSelect == null)
		{
			return false;
		}

		for(Expression e : exprsToSelect)
		{
			if(e.includesAggregateFunction())
			{
				return true;
			}
		}
		
		// JES TODO: If I am going to allow aggregate function in the ORDER BY clause then
		// this check and the semantic checks need to be updated.

		return false;
	}
	
	public boolean isOrdered()
	{
		if(orderBy.size() > 0)
		{
			return true;
		}
		return false;
	}

    
    // delegates
    // delegators


        public SQLParser(TokenStream input) {
            this(input, new RecognizerSharedState());
        }
        public SQLParser(TokenStream input, RecognizerSharedState state) {
            super(input, state);
             
        }
        

    public String[] getTokenNames() { return SQLParser.tokenNames; }
    public String getGrammarFileName() { return "SQL.g"; }



      // this is the list of expressions specified in the query's SELECT clause
      // that we need to return to the user
      private ArrayList <Expression> exprsToSelect = new ArrayList<Expression>();

      // this is a map where for each entry in the map, the key is an identifier
      // that was found in the FROM clause, and the value is a table name that
      // was found there... for example, if we have FROM myTable AS a, then the pair
      // ("a", "myTable") would be put into this map
      private Map<String, String> tables = new HashMap<String, String>();

      // this is the boolean expression in the WHERE clause
      private Expression whereClause = null;

      // this is the attribute that we are grouping by, if any (we only allow one!)
      private String groupingAtt = null;

      // this is the list of expressions that the results are ordered by
      private ArrayList<SortExpression> orderBy = new ArrayList<SortExpression>();

      // returns the list of expressions from the SELECT statement
      public ArrayList <Expression> getSELECT () {
        return exprsToSelect;
      }

      // returns the map representing the FROM clause
      public Map <String, String> getFROM () {
        return tables;
      }

      // returns the expression in the WHERE clause
      public Expression getWHERE () {
        return whereClause;
      }

      // returns the grouping attribute
      public String getGROUPBY () {
        return groupingAtt;
      }

      public ArrayList<SortExpression> getORDERBY() {
        return orderBy;
      }



    // $ANTLR start "parse"
    // SQL.g:58:1: parse returns [boolean value] : ( selectClause fromClause whereClause groupingClause orderClause ';' | 'quit' ';' );
    public final boolean parse() throws RecognitionException {
        boolean value = false;

        try {
            // SQL.g:59:2: ( selectClause fromClause whereClause groupingClause orderClause ';' | 'quit' ';' )
            int alt1=2;
            int LA1_0 = input.LA(1);

            if ( (LA1_0==11) ) {
                alt1=1;
            }
            else if ( (LA1_0==10) ) {
                alt1=2;
            }
            else {
                NoViableAltException nvae =
                    new NoViableAltException("", 1, 0, input);

                throw nvae;
            }
            switch (alt1) {
                case 1 :
                    // SQL.g:59:4: selectClause fromClause whereClause groupingClause orderClause ';'
                    {
                    pushFollow(FOLLOW_selectClause_in_parse31);
                    selectClause();

                    state._fsp--;

                    pushFollow(FOLLOW_fromClause_in_parse33);
                    fromClause();

                    state._fsp--;

                    pushFollow(FOLLOW_whereClause_in_parse35);
                    whereClause();

                    state._fsp--;

                    pushFollow(FOLLOW_groupingClause_in_parse37);
                    groupingClause();

                    state._fsp--;

                    pushFollow(FOLLOW_orderClause_in_parse39);
                    orderClause();

                    state._fsp--;

                    match(input,9,FOLLOW_9_in_parse41); 
                    value = true;

                    }
                    break;
                case 2 :
                    // SQL.g:60:4: 'quit' ';'
                    {
                    match(input,10,FOLLOW_10_in_parse48); 
                    match(input,9,FOLLOW_9_in_parse50); 
                    value = false;

                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return value;
    }
    // $ANTLR end "parse"


    // $ANTLR start "selectClause"
    // SQL.g:63:1: selectClause : 'select' exprList ;
    public final void selectClause() throws RecognitionException {
        try {
            // SQL.g:64:2: ( 'select' exprList )
            // SQL.g:64:4: 'select' exprList
            {
            match(input,11,FOLLOW_11_in_selectClause119); 
            pushFollow(FOLLOW_exprList_in_selectClause121);
            exprList();

            state._fsp--;


            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return ;
    }
    // $ANTLR end "selectClause"


    // $ANTLR start "fromClause"
    // SQL.g:67:1: fromClause : 'from' fromList ;
    public final void fromClause() throws RecognitionException {
        try {
            // SQL.g:68:2: ( 'from' fromList )
            // SQL.g:68:4: 'from' fromList
            {
            match(input,12,FOLLOW_12_in_fromClause132); 
            pushFollow(FOLLOW_fromList_in_fromClause134);
            fromList();

            state._fsp--;


            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return ;
    }
    // $ANTLR end "fromClause"


    // $ANTLR start "whereClause"
    // SQL.g:71:1: whereClause : ( 'where' c= cnfExp | );
    public final void whereClause() throws RecognitionException {
        Expression c = null;


        try {
            // SQL.g:72:2: ( 'where' c= cnfExp | )
            int alt2=2;
            int LA2_0 = input.LA(1);

            if ( (LA2_0==13) ) {
                alt2=1;
            }
            else if ( (LA2_0==9||LA2_0==14||LA2_0==17) ) {
                alt2=2;
            }
            else {
                NoViableAltException nvae =
                    new NoViableAltException("", 2, 0, input);

                throw nvae;
            }
            switch (alt2) {
                case 1 :
                    // SQL.g:72:4: 'where' c= cnfExp
                    {
                    match(input,13,FOLLOW_13_in_whereClause145); 
                    pushFollow(FOLLOW_cnfExp_in_whereClause149);
                    c=cnfExp();

                    state._fsp--;

                    whereClause = c;

                    }
                    break;
                case 2 :
                    // SQL.g:74:2: 
                    {
                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return ;
    }
    // $ANTLR end "whereClause"


    // $ANTLR start "groupingClause"
    // SQL.g:76:1: groupingClause : ( 'group' 'by' i1= Identifier '.' i2= Identifier | );
    public final void groupingClause() throws RecognitionException {
        Token i1=null;
        Token i2=null;

        try {
            // SQL.g:77:2: ( 'group' 'by' i1= Identifier '.' i2= Identifier | )
            int alt3=2;
            int LA3_0 = input.LA(1);

            if ( (LA3_0==14) ) {
                alt3=1;
            }
            else if ( (LA3_0==9||LA3_0==17) ) {
                alt3=2;
            }
            else {
                NoViableAltException nvae =
                    new NoViableAltException("", 3, 0, input);

                throw nvae;
            }
            switch (alt3) {
                case 1 :
                    // SQL.g:77:4: 'group' 'by' i1= Identifier '.' i2= Identifier
                    {
                    match(input,14,FOLLOW_14_in_groupingClause165); 
                    match(input,15,FOLLOW_15_in_groupingClause167); 
                    i1=(Token)match(input,Identifier,FOLLOW_Identifier_in_groupingClause171); 
                    match(input,16,FOLLOW_16_in_groupingClause173); 
                    i2=(Token)match(input,Identifier,FOLLOW_Identifier_in_groupingClause177); 
                    groupingAtt = (i1!=null?i1.getText():null) + "." + (i2!=null?i2.getText():null);

                    }
                    break;
                case 2 :
                    // SQL.g:79:2: 
                    {
                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return ;
    }
    // $ANTLR end "groupingClause"


    // $ANTLR start "orderClause"
    // SQL.g:81:1: orderClause : ( 'order' 'by' orderList | );
    public final void orderClause() throws RecognitionException {
        try {
            // SQL.g:82:2: ( 'order' 'by' orderList | )
            int alt4=2;
            int LA4_0 = input.LA(1);

            if ( (LA4_0==17) ) {
                alt4=1;
            }
            else if ( (LA4_0==9) ) {
                alt4=2;
            }
            else {
                NoViableAltException nvae =
                    new NoViableAltException("", 4, 0, input);

                throw nvae;
            }
            switch (alt4) {
                case 1 :
                    // SQL.g:82:4: 'order' 'by' orderList
                    {
                    match(input,17,FOLLOW_17_in_orderClause193); 
                    match(input,15,FOLLOW_15_in_orderClause195); 
                    pushFollow(FOLLOW_orderList_in_orderClause197);
                    orderList();

                    state._fsp--;


                    }
                    break;
                case 2 :
                    // SQL.g:84:2: 
                    {
                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return ;
    }
    // $ANTLR end "orderClause"


    // $ANTLR start "exprList"
    // SQL.g:86:1: exprList : e1= exp ( ',' e2= exp )* ;
    public final void exprList() throws RecognitionException {
        Expression e1 = null;

        Expression e2 = null;


        try {
            // SQL.g:87:2: (e1= exp ( ',' e2= exp )* )
            // SQL.g:87:4: e1= exp ( ',' e2= exp )*
            {
            pushFollow(FOLLOW_exp_in_exprList213);
            e1=exp();

            state._fsp--;

            exprsToSelect.add (e1);
            // SQL.g:88:3: ( ',' e2= exp )*
            loop5:
            do {
                int alt5=2;
                int LA5_0 = input.LA(1);

                if ( (LA5_0==18) ) {
                    alt5=1;
                }


                switch (alt5) {
            	case 1 :
            	    // SQL.g:88:5: ',' e2= exp
            	    {
            	    match(input,18,FOLLOW_18_in_exprList227); 
            	    pushFollow(FOLLOW_exp_in_exprList231);
            	    e2=exp();

            	    state._fsp--;

            	    exprsToSelect.add (e2);

            	    }
            	    break;

            	default :
            	    break loop5;
                }
            } while (true);


            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return ;
    }
    // $ANTLR end "exprList"


    // $ANTLR start "orderList"
    // SQL.g:92:1: orderList : e1= exp d1= dir ( ',' e2= exp d2= dir )* ;
    public final void orderList() throws RecognitionException {
        Expression e1 = null;

        boolean d1 = false;

        Expression e2 = null;

        boolean d2 = false;


        try {
            // SQL.g:93:2: (e1= exp d1= dir ( ',' e2= exp d2= dir )* )
            // SQL.g:93:4: e1= exp d1= dir ( ',' e2= exp d2= dir )*
            {
            pushFollow(FOLLOW_exp_in_orderList252);
            e1=exp();

            state._fsp--;

            pushFollow(FOLLOW_dir_in_orderList256);
            d1=dir();

            state._fsp--;

            orderBy.add (new SortExpression(e1, d1));
            // SQL.g:94:3: ( ',' e2= exp d2= dir )*
            loop6:
            do {
                int alt6=2;
                int LA6_0 = input.LA(1);

                if ( (LA6_0==18) ) {
                    alt6=1;
                }


                switch (alt6) {
            	case 1 :
            	    // SQL.g:94:5: ',' e2= exp d2= dir
            	    {
            	    match(input,18,FOLLOW_18_in_orderList273); 
            	    pushFollow(FOLLOW_exp_in_orderList277);
            	    e2=exp();

            	    state._fsp--;

            	    pushFollow(FOLLOW_dir_in_orderList281);
            	    d2=dir();

            	    state._fsp--;

            	    orderBy.add (new SortExpression(e2, d2));

            	    }
            	    break;

            	default :
            	    break loop6;
                }
            } while (true);


            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return ;
    }
    // $ANTLR end "orderList"


    // $ANTLR start "fromList"
    // SQL.g:98:1: fromList : i1= Identifier 'as' i2= Identifier ( ',' i3= Identifier 'as' i4= Identifier )* ;
    public final void fromList() throws RecognitionException {
        Token i1=null;
        Token i2=null;
        Token i3=null;
        Token i4=null;

        try {
            // SQL.g:99:2: (i1= Identifier 'as' i2= Identifier ( ',' i3= Identifier 'as' i4= Identifier )* )
            // SQL.g:99:4: i1= Identifier 'as' i2= Identifier ( ',' i3= Identifier 'as' i4= Identifier )*
            {
            i1=(Token)match(input,Identifier,FOLLOW_Identifier_in_fromList305); 
            match(input,19,FOLLOW_19_in_fromList307); 
            i2=(Token)match(input,Identifier,FOLLOW_Identifier_in_fromList311); 
            tables.put ((i2!=null?i2.getText():null), (i1!=null?i1.getText():null));
            // SQL.g:100:3: ( ',' i3= Identifier 'as' i4= Identifier )*
            loop7:
            do {
                int alt7=2;
                int LA7_0 = input.LA(1);

                if ( (LA7_0==18) ) {
                    alt7=1;
                }


                switch (alt7) {
            	case 1 :
            	    // SQL.g:100:5: ',' i3= Identifier 'as' i4= Identifier
            	    {
            	    match(input,18,FOLLOW_18_in_fromList325); 
            	    i3=(Token)match(input,Identifier,FOLLOW_Identifier_in_fromList329); 
            	    match(input,19,FOLLOW_19_in_fromList331); 
            	    i4=(Token)match(input,Identifier,FOLLOW_Identifier_in_fromList335); 
            	    tables.put ((i4!=null?i4.getText():null), (i3!=null?i3.getText():null));

            	    }
            	    break;

            	default :
            	    break loop7;
                }
            } while (true);


            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return ;
    }
    // $ANTLR end "fromList"


    // $ANTLR start "cnfExp"
    // SQL.g:104:1: cnfExp returns [Expression value] : d1= disjunction ( 'and' d2= disjunction )* ;
    public final Expression cnfExp() throws RecognitionException {
        Expression value = null;

        Expression d1 = null;

        Expression d2 = null;


        try {
            // SQL.g:105:2: (d1= disjunction ( 'and' d2= disjunction )* )
            // SQL.g:105:4: d1= disjunction ( 'and' d2= disjunction )*
            {
            pushFollow(FOLLOW_disjunction_in_cnfExp360);
            d1=disjunction();

            state._fsp--;

            value = d1;
            // SQL.g:106:3: ( 'and' d2= disjunction )*
            loop8:
            do {
                int alt8=2;
                int LA8_0 = input.LA(1);

                if ( (LA8_0==20) ) {
                    alt8=1;
                }


                switch (alt8) {
            	case 1 :
            	    // SQL.g:106:5: 'and' d2= disjunction
            	    {
            	    match(input,20,FOLLOW_20_in_cnfExp370); 
            	    pushFollow(FOLLOW_disjunction_in_cnfExp374);
            	    d2=disjunction();

            	    state._fsp--;

            	    Expression temp = value;
            	    					 value = new Expression ("and");
            	    					 value.setSubexpression (temp, d2);

            	    }
            	    break;

            	default :
            	    break loop8;
                }
            } while (true);


            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return value;
    }
    // $ANTLR end "cnfExp"


    // $ANTLR start "disjunction"
    // SQL.g:112:1: disjunction returns [Expression value] : '(' c1= comparison ( 'or' c2= comparison )* ')' ;
    public final Expression disjunction() throws RecognitionException {
        Expression value = null;

        Expression c1 = null;

        Expression c2 = null;


        try {
            // SQL.g:113:2: ( '(' c1= comparison ( 'or' c2= comparison )* ')' )
            // SQL.g:113:4: '(' c1= comparison ( 'or' c2= comparison )* ')'
            {
            match(input,21,FOLLOW_21_in_disjunction396); 
            pushFollow(FOLLOW_comparison_in_disjunction400);
            c1=comparison();

            state._fsp--;

            value = c1;
            // SQL.g:114:3: ( 'or' c2= comparison )*
            loop9:
            do {
                int alt9=2;
                int LA9_0 = input.LA(1);

                if ( (LA9_0==22) ) {
                    alt9=1;
                }


                switch (alt9) {
            	case 1 :
            	    // SQL.g:114:5: 'or' c2= comparison
            	    {
            	    match(input,22,FOLLOW_22_in_disjunction409); 
            	    pushFollow(FOLLOW_comparison_in_disjunction413);
            	    c2=comparison();

            	    state._fsp--;

            	    Expression temp = value;
            	    					 value = new Expression ("or");
            	    					 value.setSubexpression (temp, c2);

            	    }
            	    break;

            	default :
            	    break loop9;
                }
            } while (true);

            match(input,23,FOLLOW_23_in_disjunction422); 

            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return value;
    }
    // $ANTLR end "disjunction"


    // $ANTLR start "comparison"
    // SQL.g:120:1: comparison returns [Expression value] : (e1= addExp ( '=' e2= addExp | '<' e2= addExp | '>' e2= addExp ) | 'not' c= comparison );
    public final Expression comparison() throws RecognitionException {
        Expression value = null;

        Expression e1 = null;

        Expression e2 = null;

        Expression c = null;


        try {
            // SQL.g:121:2: (e1= addExp ( '=' e2= addExp | '<' e2= addExp | '>' e2= addExp ) | 'not' c= comparison )
            int alt11=2;
            int LA11_0 = input.LA(1);

            if ( ((LA11_0>=Identifier && LA11_0<=Float)||LA11_0==21||LA11_0==33) ) {
                alt11=1;
            }
            else if ( (LA11_0==27) ) {
                alt11=2;
            }
            else {
                NoViableAltException nvae =
                    new NoViableAltException("", 11, 0, input);

                throw nvae;
            }
            switch (alt11) {
                case 1 :
                    // SQL.g:121:4: e1= addExp ( '=' e2= addExp | '<' e2= addExp | '>' e2= addExp )
                    {
                    pushFollow(FOLLOW_addExp_in_comparison439);
                    e1=addExp();

                    state._fsp--;

                    value = e1;
                    // SQL.g:122:3: ( '=' e2= addExp | '<' e2= addExp | '>' e2= addExp )
                    int alt10=3;
                    switch ( input.LA(1) ) {
                    case 24:
                        {
                        alt10=1;
                        }
                        break;
                    case 25:
                        {
                        alt10=2;
                        }
                        break;
                    case 26:
                        {
                        alt10=3;
                        }
                        break;
                    default:
                        NoViableAltException nvae =
                            new NoViableAltException("", 10, 0, input);

                        throw nvae;
                    }

                    switch (alt10) {
                        case 1 :
                            // SQL.g:122:5: '=' e2= addExp
                            {
                            match(input,24,FOLLOW_24_in_comparison448); 
                            pushFollow(FOLLOW_addExp_in_comparison452);
                            e2=addExp();

                            state._fsp--;

                            Expression temp = value;
                            				 value = new Expression ("equals");
                            				 value.setSubexpression (temp, e2);

                            }
                            break;
                        case 2 :
                            // SQL.g:125:5: '<' e2= addExp
                            {
                            match(input,25,FOLLOW_25_in_comparison460); 
                            pushFollow(FOLLOW_addExp_in_comparison464);
                            e2=addExp();

                            state._fsp--;

                            Expression temp = value;
                            				 value = new Expression ("less than");
                            				 value.setSubexpression (temp, e2);

                            }
                            break;
                        case 3 :
                            // SQL.g:128:5: '>' e2= addExp
                            {
                            match(input,26,FOLLOW_26_in_comparison472); 
                            pushFollow(FOLLOW_addExp_in_comparison476);
                            e2=addExp();

                            state._fsp--;

                            Expression temp = value;
                            				 value = new Expression ("greater than");
                            				 value.setSubexpression (temp, e2);

                            }
                            break;

                    }


                    }
                    break;
                case 2 :
                    // SQL.g:132:5: 'not' c= comparison
                    {
                    match(input,27,FOLLOW_27_in_comparison488); 
                    pushFollow(FOLLOW_comparison_in_comparison492);
                    c=comparison();

                    state._fsp--;

                    value = new Expression ("not");
                    				    value.setSubexpression (c);

                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return value;
    }
    // $ANTLR end "comparison"


    // $ANTLR start "exp"
    // SQL.g:136:1: exp returns [Expression value] : ( 'sum' '(' e1= addExp ')' | 'avg' '(' e1= addExp ')' | e1= addExp );
    public final Expression exp() throws RecognitionException {
        Expression value = null;

        Expression e1 = null;


        try {
            // SQL.g:137:2: ( 'sum' '(' e1= addExp ')' | 'avg' '(' e1= addExp ')' | e1= addExp )
            int alt12=3;
            switch ( input.LA(1) ) {
            case 28:
                {
                alt12=1;
                }
                break;
            case 29:
                {
                alt12=2;
                }
                break;
            case Identifier:
            case Int:
            case CharString:
            case Float:
            case 21:
            case 33:
                {
                alt12=3;
                }
                break;
            default:
                NoViableAltException nvae =
                    new NoViableAltException("", 12, 0, input);

                throw nvae;
            }

            switch (alt12) {
                case 1 :
                    // SQL.g:137:4: 'sum' '(' e1= addExp ')'
                    {
                    match(input,28,FOLLOW_28_in_exp509); 
                    match(input,21,FOLLOW_21_in_exp511); 
                    pushFollow(FOLLOW_addExp_in_exp515);
                    e1=addExp();

                    state._fsp--;

                    match(input,23,FOLLOW_23_in_exp517); 
                    value = new Expression ("sum");
                    				         value.setSubexpression (e1);

                    }
                    break;
                case 2 :
                    // SQL.g:139:4: 'avg' '(' e1= addExp ')'
                    {
                    match(input,29,FOLLOW_29_in_exp524); 
                    match(input,21,FOLLOW_21_in_exp526); 
                    pushFollow(FOLLOW_addExp_in_exp530);
                    e1=addExp();

                    state._fsp--;

                    match(input,23,FOLLOW_23_in_exp532); 
                    value = new Expression ("avg");
                    				         value.setSubexpression (e1);

                    }
                    break;
                case 3 :
                    // SQL.g:141:4: e1= addExp
                    {
                    pushFollow(FOLLOW_addExp_in_exp541);
                    e1=addExp();

                    state._fsp--;

                    value = e1;

                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return value;
    }
    // $ANTLR end "exp"


    // $ANTLR start "dir"
    // SQL.g:144:1: dir returns [boolean value] : ( 'asc' | 'dsc' | );
    public final boolean dir() throws RecognitionException {
        boolean value = false;

        try {
            // SQL.g:145:2: ( 'asc' | 'dsc' | )
            int alt13=3;
            switch ( input.LA(1) ) {
            case 30:
                {
                alt13=1;
                }
                break;
            case 31:
                {
                alt13=2;
                }
                break;
            case 9:
            case 18:
                {
                alt13=3;
                }
                break;
            default:
                NoViableAltException nvae =
                    new NoViableAltException("", 13, 0, input);

                throw nvae;
            }

            switch (alt13) {
                case 1 :
                    // SQL.g:145:4: 'asc'
                    {
                    match(input,30,FOLLOW_30_in_dir572); 
                    value = true;

                    }
                    break;
                case 2 :
                    // SQL.g:146:4: 'dsc'
                    {
                    match(input,31,FOLLOW_31_in_dir579); 
                    value = false;

                    }
                    break;
                case 3 :
                    // SQL.g:147:4: 
                    {
                    value = true;

                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return value;
    }
    // $ANTLR end "dir"


    // $ANTLR start "addExp"
    // SQL.g:150:1: addExp returns [Expression value] : m1= multExp ( '+' m2= multExp | '-' m2= multExp )* ;
    public final Expression addExp() throws RecognitionException {
        Expression value = null;

        Expression m1 = null;

        Expression m2 = null;


        try {
            // SQL.g:151:2: (m1= multExp ( '+' m2= multExp | '-' m2= multExp )* )
            // SQL.g:151:4: m1= multExp ( '+' m2= multExp | '-' m2= multExp )*
            {
            pushFollow(FOLLOW_multExp_in_addExp603);
            m1=multExp();

            state._fsp--;

            value = m1;
            // SQL.g:152:3: ( '+' m2= multExp | '-' m2= multExp )*
            loop14:
            do {
                int alt14=3;
                int LA14_0 = input.LA(1);

                if ( (LA14_0==32) ) {
                    alt14=1;
                }
                else if ( (LA14_0==33) ) {
                    alt14=2;
                }


                switch (alt14) {
            	case 1 :
            	    // SQL.g:152:5: '+' m2= multExp
            	    {
            	    match(input,32,FOLLOW_32_in_addExp620); 
            	    pushFollow(FOLLOW_multExp_in_addExp624);
            	    m2=multExp();

            	    state._fsp--;

            	    Expression temp = value;
            	    				     value = new Expression ("plus");
            	    				     value.setSubexpression (temp, m2);

            	    }
            	    break;
            	case 2 :
            	    // SQL.g:155:5: '-' m2= multExp
            	    {
            	    match(input,33,FOLLOW_33_in_addExp635); 
            	    pushFollow(FOLLOW_multExp_in_addExp639);
            	    m2=multExp();

            	    state._fsp--;

            	    Expression temp = value;
            	    				     value = new Expression ("minus");
            	    				     value.setSubexpression (temp, m2);

            	    }
            	    break;

            	default :
            	    break loop14;
                }
            } while (true);


            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return value;
    }
    // $ANTLR end "addExp"


    // $ANTLR start "multExp"
    // SQL.g:161:1: multExp returns [Expression value] : m1= atomExp ( '*' m2= atomExp | '/' m2= atomExp )* ;
    public final Expression multExp() throws RecognitionException {
        Expression value = null;

        Expression m1 = null;

        Expression m2 = null;


        try {
            // SQL.g:162:2: (m1= atomExp ( '*' m2= atomExp | '/' m2= atomExp )* )
            // SQL.g:162:4: m1= atomExp ( '*' m2= atomExp | '/' m2= atomExp )*
            {
            pushFollow(FOLLOW_atomExp_in_multExp666);
            m1=atomExp();

            state._fsp--;

            value = m1;
            // SQL.g:163:3: ( '*' m2= atomExp | '/' m2= atomExp )*
            loop15:
            do {
                int alt15=3;
                int LA15_0 = input.LA(1);

                if ( (LA15_0==34) ) {
                    alt15=1;
                }
                else if ( (LA15_0==35) ) {
                    alt15=2;
                }


                switch (alt15) {
            	case 1 :
            	    // SQL.g:163:5: '*' m2= atomExp
            	    {
            	    match(input,34,FOLLOW_34_in_multExp683); 
            	    pushFollow(FOLLOW_atomExp_in_multExp687);
            	    m2=atomExp();

            	    state._fsp--;

            	    Expression temp = value;
            	    				     value = new Expression ("times");
            	    				     value.setSubexpression (temp, m2);

            	    }
            	    break;
            	case 2 :
            	    // SQL.g:166:5: '/' m2= atomExp
            	    {
            	    match(input,35,FOLLOW_35_in_multExp698); 
            	    pushFollow(FOLLOW_atomExp_in_multExp702);
            	    m2=atomExp();

            	    state._fsp--;

            	    Expression temp = value;
            	    				     value = new Expression ("divided by");
            	    				     value.setSubexpression (temp, m2);

            	    }
            	    break;

            	default :
            	    break loop15;
                }
            } while (true);


            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return value;
    }
    // $ANTLR end "multExp"


    // $ANTLR start "atomExp"
    // SQL.g:172:1: atomExp returns [Expression value] : (n= Int | i1= Identifier '.' i2= Identifier | c= CharString | c= Float | '(' e1= addExp ')' | '-' e1= atomExp );
    public final Expression atomExp() throws RecognitionException {
        Expression value = null;

        Token n=null;
        Token i1=null;
        Token i2=null;
        Token c=null;
        Expression e1 = null;


        try {
            // SQL.g:173:2: (n= Int | i1= Identifier '.' i2= Identifier | c= CharString | c= Float | '(' e1= addExp ')' | '-' e1= atomExp )
            int alt16=6;
            switch ( input.LA(1) ) {
            case Int:
                {
                alt16=1;
                }
                break;
            case Identifier:
                {
                alt16=2;
                }
                break;
            case CharString:
                {
                alt16=3;
                }
                break;
            case Float:
                {
                alt16=4;
                }
                break;
            case 21:
                {
                alt16=5;
                }
                break;
            case 33:
                {
                alt16=6;
                }
                break;
            default:
                NoViableAltException nvae =
                    new NoViableAltException("", 16, 0, input);

                throw nvae;
            }

            switch (alt16) {
                case 1 :
                    // SQL.g:173:4: n= Int
                    {
                    n=(Token)match(input,Int,FOLLOW_Int_in_atomExp729); 
                    value = new Expression ("literal int");
                    				     value.setValue ((n!=null?n.getText():null));

                    }
                    break;
                case 2 :
                    // SQL.g:175:4: i1= Identifier '.' i2= Identifier
                    {
                    i1=(Token)match(input,Identifier,FOLLOW_Identifier_in_atomExp745); 
                    match(input,16,FOLLOW_16_in_atomExp747); 
                    i2=(Token)match(input,Identifier,FOLLOW_Identifier_in_atomExp751); 
                    value = new Expression ("identifier");
                    				     			     value.setValue ((i1!=null?i1.getText():null) + "." + (i2!=null?i2.getText():null));

                    }
                    break;
                case 3 :
                    // SQL.g:177:4: c= CharString
                    {
                    c=(Token)match(input,CharString,FOLLOW_CharString_in_atomExp765); 
                    value = new Expression ("literal string");
                    				     value.setValue ((c!=null?c.getText():null));

                    }
                    break;
                case 4 :
                    // SQL.g:179:4: c= Float
                    {
                    c=(Token)match(input,Float,FOLLOW_Float_in_atomExp778); 
                    value = new Expression ("literal float");
                    				     value.setValue ((c!=null?c.getText():null));

                    }
                    break;
                case 5 :
                    // SQL.g:181:4: '(' e1= addExp ')'
                    {
                    match(input,21,FOLLOW_21_in_atomExp790); 
                    pushFollow(FOLLOW_addExp_in_atomExp794);
                    e1=addExp();

                    state._fsp--;

                    match(input,23,FOLLOW_23_in_atomExp796); 
                    value = e1;

                    }
                    break;
                case 6 :
                    // SQL.g:182:5: '-' e1= atomExp
                    {
                    match(input,33,FOLLOW_33_in_atomExp806); 
                    pushFollow(FOLLOW_atomExp_in_atomExp810);
                    e1=atomExp();

                    state._fsp--;

                    value = new Expression ("unary minus");
                    				     value.setSubexpression (e1);

                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return value;
    }
    // $ANTLR end "atomExp"

    // Delegated rules


 

    public static final BitSet FOLLOW_selectClause_in_parse31 = new BitSet(new long[]{0x0000000000001000L});
    public static final BitSet FOLLOW_fromClause_in_parse33 = new BitSet(new long[]{0x0000000000026200L});
    public static final BitSet FOLLOW_whereClause_in_parse35 = new BitSet(new long[]{0x0000000000024200L});
    public static final BitSet FOLLOW_groupingClause_in_parse37 = new BitSet(new long[]{0x0000000000020200L});
    public static final BitSet FOLLOW_orderClause_in_parse39 = new BitSet(new long[]{0x0000000000000200L});
    public static final BitSet FOLLOW_9_in_parse41 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_10_in_parse48 = new BitSet(new long[]{0x0000000000000200L});
    public static final BitSet FOLLOW_9_in_parse50 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_11_in_selectClause119 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_exprList_in_selectClause121 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_12_in_fromClause132 = new BitSet(new long[]{0x0000000000000010L});
    public static final BitSet FOLLOW_fromList_in_fromClause134 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_13_in_whereClause145 = new BitSet(new long[]{0x0000000000200000L});
    public static final BitSet FOLLOW_cnfExp_in_whereClause149 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_14_in_groupingClause165 = new BitSet(new long[]{0x0000000000008000L});
    public static final BitSet FOLLOW_15_in_groupingClause167 = new BitSet(new long[]{0x0000000000000010L});
    public static final BitSet FOLLOW_Identifier_in_groupingClause171 = new BitSet(new long[]{0x0000000000010000L});
    public static final BitSet FOLLOW_16_in_groupingClause173 = new BitSet(new long[]{0x0000000000000010L});
    public static final BitSet FOLLOW_Identifier_in_groupingClause177 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_17_in_orderClause193 = new BitSet(new long[]{0x0000000000008000L});
    public static final BitSet FOLLOW_15_in_orderClause195 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_orderList_in_orderClause197 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_exp_in_exprList213 = new BitSet(new long[]{0x0000000000040002L});
    public static final BitSet FOLLOW_18_in_exprList227 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_exp_in_exprList231 = new BitSet(new long[]{0x0000000000040002L});
    public static final BitSet FOLLOW_exp_in_orderList252 = new BitSet(new long[]{0x00000000C0040000L});
    public static final BitSet FOLLOW_dir_in_orderList256 = new BitSet(new long[]{0x0000000000040002L});
    public static final BitSet FOLLOW_18_in_orderList273 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_exp_in_orderList277 = new BitSet(new long[]{0x00000000C0040000L});
    public static final BitSet FOLLOW_dir_in_orderList281 = new BitSet(new long[]{0x0000000000040002L});
    public static final BitSet FOLLOW_Identifier_in_fromList305 = new BitSet(new long[]{0x0000000000080000L});
    public static final BitSet FOLLOW_19_in_fromList307 = new BitSet(new long[]{0x0000000000000010L});
    public static final BitSet FOLLOW_Identifier_in_fromList311 = new BitSet(new long[]{0x0000000000040002L});
    public static final BitSet FOLLOW_18_in_fromList325 = new BitSet(new long[]{0x0000000000000010L});
    public static final BitSet FOLLOW_Identifier_in_fromList329 = new BitSet(new long[]{0x0000000000080000L});
    public static final BitSet FOLLOW_19_in_fromList331 = new BitSet(new long[]{0x0000000000000010L});
    public static final BitSet FOLLOW_Identifier_in_fromList335 = new BitSet(new long[]{0x0000000000040002L});
    public static final BitSet FOLLOW_disjunction_in_cnfExp360 = new BitSet(new long[]{0x0000000000100002L});
    public static final BitSet FOLLOW_20_in_cnfExp370 = new BitSet(new long[]{0x0000000000200000L});
    public static final BitSet FOLLOW_disjunction_in_cnfExp374 = new BitSet(new long[]{0x0000000000100002L});
    public static final BitSet FOLLOW_21_in_disjunction396 = new BitSet(new long[]{0x00000002382000F0L});
    public static final BitSet FOLLOW_comparison_in_disjunction400 = new BitSet(new long[]{0x0000000000C00000L});
    public static final BitSet FOLLOW_22_in_disjunction409 = new BitSet(new long[]{0x00000002382000F0L});
    public static final BitSet FOLLOW_comparison_in_disjunction413 = new BitSet(new long[]{0x0000000000C00000L});
    public static final BitSet FOLLOW_23_in_disjunction422 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_addExp_in_comparison439 = new BitSet(new long[]{0x0000000007000000L});
    public static final BitSet FOLLOW_24_in_comparison448 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_addExp_in_comparison452 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_25_in_comparison460 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_addExp_in_comparison464 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_26_in_comparison472 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_addExp_in_comparison476 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_27_in_comparison488 = new BitSet(new long[]{0x00000002382000F0L});
    public static final BitSet FOLLOW_comparison_in_comparison492 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_28_in_exp509 = new BitSet(new long[]{0x0000000000200000L});
    public static final BitSet FOLLOW_21_in_exp511 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_addExp_in_exp515 = new BitSet(new long[]{0x0000000000800000L});
    public static final BitSet FOLLOW_23_in_exp517 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_29_in_exp524 = new BitSet(new long[]{0x0000000000200000L});
    public static final BitSet FOLLOW_21_in_exp526 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_addExp_in_exp530 = new BitSet(new long[]{0x0000000000800000L});
    public static final BitSet FOLLOW_23_in_exp532 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_addExp_in_exp541 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_30_in_dir572 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_31_in_dir579 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_multExp_in_addExp603 = new BitSet(new long[]{0x0000000300000002L});
    public static final BitSet FOLLOW_32_in_addExp620 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_multExp_in_addExp624 = new BitSet(new long[]{0x0000000300000002L});
    public static final BitSet FOLLOW_33_in_addExp635 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_multExp_in_addExp639 = new BitSet(new long[]{0x0000000300000002L});
    public static final BitSet FOLLOW_atomExp_in_multExp666 = new BitSet(new long[]{0x0000000C00000002L});
    public static final BitSet FOLLOW_34_in_multExp683 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_atomExp_in_multExp687 = new BitSet(new long[]{0x0000000C00000002L});
    public static final BitSet FOLLOW_35_in_multExp698 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_atomExp_in_multExp702 = new BitSet(new long[]{0x0000000C00000002L});
    public static final BitSet FOLLOW_Int_in_atomExp729 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_Identifier_in_atomExp745 = new BitSet(new long[]{0x0000000000010000L});
    public static final BitSet FOLLOW_16_in_atomExp747 = new BitSet(new long[]{0x0000000000000010L});
    public static final BitSet FOLLOW_Identifier_in_atomExp751 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_CharString_in_atomExp765 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_Float_in_atomExp778 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_21_in_atomExp790 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_addExp_in_atomExp794 = new BitSet(new long[]{0x0000000000800000L});
    public static final BitSet FOLLOW_23_in_atomExp796 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_33_in_atomExp806 = new BitSet(new long[]{0x00000002302000F0L});
    public static final BitSet FOLLOW_atomExp_in_atomExp810 = new BitSet(new long[]{0x0000000000000002L});

}